package TextRank;

import java.util.*;

public class Order {

    //关键句排序并保持源码格式
    public List<String> Order(String str,List<String> keySentences){
        List <String> result = new ArrayList<String>();
        String lines = "";
        //存储每行代码次序
        Map<Integer,String> ma = new LinkedHashMap<Integer,String>();
        //存储该行代码是否取了，取了为1，没取为0
        Map<Integer,Integer> ma2 = new LinkedHashMap<>();
        int num =1;

        //使用Scanner来读取字符串
        Scanner sc = new Scanner(str);
        String line;
        while (sc.hasNextLine()) {
            line = sc.nextLine();
            ma.put(num,line);
            ma2.put(num,0);
            num++;
        }

        //存储代码行对应的数值
        int order[] = new int[keySentences.size()];
        int temp = 0;
        for (String i :keySentences) {
            for (Map.Entry<Integer,String> entry:ma.entrySet()) {
                if (entry.getValue().contains(i) && ma2.get(entry.getKey()) == 0){   //ma2行数对应的bool为0
                    order[temp++] = entry.getKey();
                    ma2.put(entry.getKey(),1);
                    break;
                }
            }
        }
        Arrays.sort(order);
        for (int i :order){
            for (Map.Entry<Integer,String> entry:ma.entrySet()) {
                if (i == entry.getKey()){
                    result.add(entry.getValue());
                    break;
                }
            }
        }
        return result;
    }
}
